<?php

namespace app\api\model;

use think\Model;

class AutoCategory extends Model
{

    public static function getCategory()
    {
        $categories = self::with(['services' => function ($query) {
            $query->with('service');
        }])->where('onlineswitch', 1)->order('weigh desc')->select();
        foreach ($categories as $val) {
            if ($val['services']) {
                foreach ($val['services'] as $v) {
                    $v['service_name'] = $v['service']['service_name'];
                    $v['service_price'] = $v['service']['service_price'];
                    unset($v['service']);
                }
            }
        }
        return $categories;
    }

    public function services()
    {
        return $this->hasMany(AutoService::class, 'auto_category_id', 'id')->order('weigh desc');
    }

}
